﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Unleashed.AutoUpdate.CustomActions;
using Unleashed.AutoUpdate.IO.Configuration;
using System.Reflection;
using System.IO;

namespace Unleashed.AutoUpdate.Common.Test.CustomActions
{
    [TestClass]
    public class ReflectionLoaderTest
    {
        [TestMethod]
        [DeploymentItem("Data//Config_CustomActions_My.xml")]
        public void LoadInstanceTest()
        {
            string cfgPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config_CustomActions_My.xml");
            UpdateConfig cfg = UpdateConfig.Load(cfgPath);
            string assemblyPath = Path.Combine(Path.GetDirectoryName(cfgPath), cfg.CustomActionAssemblyName + ".dll");

            object a = ReflectionLoader.LoadInstance(assemblyPath, cfg.CustomActionTypeName);
            Assert.IsNotNull(a);
        }

        [TestMethod]
        [DeploymentItem("Data//Config_CustomActions_Error.xml")]
        public void LoadInstanceErrorTest()
        {
            string cfgPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config_CustomActions_Error.xml");
            UpdateConfig cfg = UpdateConfig.Load(cfgPath);
            string assemblyPath = Path.Combine(Path.GetDirectoryName(cfgPath), cfg.CustomActionAssemblyName + ".dll");

            object a = ReflectionLoader.LoadInstance(assemblyPath, cfg.CustomActionTypeName);
            Assert.IsNull(a);
        }

        [TestMethod]
        [DeploymentItem("Data//Config_CustomActions_NotDerived.xml")]
        public void LoadInstanceNotDerivedTest()
        {
            string cfgPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config_CustomActions_NotDerived.xml");
            UpdateConfig cfg = UpdateConfig.Load(cfgPath);
            string assemblyPath = Path.Combine(Path.GetDirectoryName(cfgPath), cfg.CustomActionAssemblyName + ".dll");

            object a = ReflectionLoader.LoadInstance(assemblyPath, cfg.CustomActionTypeName);
            Assert.IsNull(a);
        }
    }
}